package cheme.ProcessModel.ProcessUnits.Streams.Tags;

import java.io.IOException;
import java.io.Serializable;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import cheme.ProcessModel.IChild;
import cheme.ProcessModel.IModelElement;
import cheme.ProcessModel.ModelEnv;
import cheme.ProcessModel.XmlHelper;
import cheme.ProcessModel.Table.ImmutableField;
import cheme.ProcessModel.Visitors.IModelVisitor;

public class OverallTagComponent extends AbstractTagComponent implements IOverallTagComponent, Serializable
{
	/*
	 * Serialization version identifier. (Set to default for now)
	 */
	
	private static final long serialVersionUID = 1L;
	
	public OverallTagComponent(IModelElement parent)
	{
		super(parent);
		
		initializing = false;

		get(LABEL_INDEX).setToolTipText("The name used to refer to this overall stream in yuour chemical process.\n" +
										"You can build equations involving this stream component by dragging and dropping this label into the equations editor.");
	
		get(QUANTITY_INDEX).setToolTipText("The overall quantity for this stream.");

		OverallUnitsField units = new OverallUnitsField(this, 0);
		units.setToolTipText("The basis unit of measurement for this stream.");
		add(units, UNITS_INDEX);
			
		ImmutableField material = new ImmutableField(this, "Overall");//Pawan Change here to get Overall under material
		material.setToolTipText("This is a descriptor for the entire stream.\nPlease enter inidivdual stream materials in the rows below.");
		
		add(material, MATERIAL_INDEX);
		
		setInitial(false);
	}
	
	private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException
	{
		/*
		 * Make sure the interpreter can listen to my changes
		 */

		ModelEnv.getInstanceFor(this).hookModelElement(this);
		
		/*
		 * Make sure I can listen to my children created form a past session.
		 */

		for(IChild child : getChildren())
		{
			child.addPropertyChangeListener(this);
		}
	}
	
	/*
	 * Overall components are by default never initial
	 */
	public void initialize(ITagComponentInitPolicy initPolicy)
	{	
		//mark as init
		setInitial(false);		
	}
	
	public Object accept(IModelVisitor modelVisitor)
	{
		return modelVisitor.visit(this);
	}
	
public String toXml() {
		
		// we must modify "type" attribute from the
		//base class.  We need to make sure that the change
		//is reflected in our XML
		String xml = super.toXml();
		Document doc = (Document)XmlHelper.xmlFromString(xml);
		Element e1 = doc.getDocumentElement();
		e1.setAttribute("type", "OverallTagComponent");
		
		xml = XmlHelper.xmlToString(doc);
		return xml;
	}
	
	/*public OverallTagComponent accept(ModelTypeResolver modelResolver)
	{
		return modelResolver.visit(this);
	}*/
}
